package smallchangeOOP;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class SmallChangeOOP {
    /*
        项目需求
        使用java开发 零钱通项目，可以完成收益入账，消费，
        查看明细，退出系统等功能
     */

    //属性
    boolean loop = true;
    double money = 0;
    double balance = 0;//余额
    String details = " ";
    Date date = null;//data 是 java.util.Data类型，表示日期
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    String note = "";

    Scanner scanner = new Scanner(System.in);
    String key = "";

    //完成显示菜单
    public void mainManu() {
        do {
            System.out.println("----------选择零钱通菜单---------");
            System.out.println("\t\t1 零钱通明细");
            System.out.println("\t\t2 收益入账");
            System.out.println("\t\t3 消费");
            System.out.println("\t\t4 退出");

            System.out.println("请选择1-4");
            key = scanner.next();
            //使用swicth 分支控制
            switch (key) {
                case "1":
                    this.detail();
                    break;
                case "2":
                    this.income();
                    break;
                case "3":
                    this.pay();
                    break;
                case "4":
                    this.exit();
                    break;
                default:
                    System.out.println("菜单选择有误，请重新选择");
            }
        } while (loop);
        System.out.println("退出了零钱通项目");
    }


    //完成零钱通明细
    public void detail() {
        System.out.println(details);
    }

    //完成收益入账
    public void income() {
        System.out.println("收益入账金额");
        money = scanner.nextDouble();
        //找出不正确的金额条件，然后给出提示，就break
        if (money <= 0) {
            System.out.println("收益金额 需要大于0");
            return;
        }
        balance += money;
        date = new Date();//获取当前的日期
        details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t余额：" + balance;
    }

    //完成消费
    public void pay() {
        System.out.println("消费金额：");
        money = scanner.nextDouble();
        //找出金额不正确的情况
        if (money > balance) {
            System.out.println("消费金额应该在 0-" + balance);
            return;
        }
        balance -= money;
        System.out.println("消费说明：");
        note = scanner.next();
        details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t余额：" + balance;
    }

    //退出
    public void exit() {
        String choice;
        while (true) {//该循环主要完成 必须输入正确的y/n，否则循环输入指令
            System.out.println("你确认要退出吗？y/n");
            choice = scanner.next();
            if ("y".equals(choice) || "n".equals(choice)) {
                break;
            }
        }
        //判断
        if (choice.equals("y")) {
            loop = false;
        }
    }
}

